<div class="layui-fluid layui-anim febs-anim" id="febs-bookCart" lay-title="我的购物车"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="bookCart-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="bookname" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书类型</label>
                    <div class="layui-input-inline">
                      <select name="type"
                              lay-verify="required"
                              xm-select-direction="down"
                              xm-select="insure-type"
                              xm-select-skin="default">
                      </select>
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="bookCart:update">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="bookCartTable" lay-data="{id: 'bookCartTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="sell-cart">
  {{#
  var cart = {
  1: {title: 'YES', color: 'green'},
  0: {title: 'No', color: 'volcano'},
  }[d.cart];
  }}
  <span class="layui-badge febs-tag-{{cart.color}}">{{ cart.title }}</span>
</script>
<script type="text/html" id="sell-love">
  {{#
  var love = {
  1: {title: '已收藏', color: 'green'},
  0: {title: '未收藏', color: 'volcano'},
  }[d.love];
  }}
  <span class="layui-badge febs-tag-{{love.color}}">{{ love.title }}</span>
</script>
<script type="text/html" id="bookCart-option-one" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="bookCart:view,bookCart:update,bookCart:delete,bookCart:pay">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="edit" shiro:hasPermission="bookCart:update"><i
          class="layui-icon layui-anim-fadein febs-edit-area febs-blue">&#xe7a4;</i></a>
  <a lay-event="del" shiro:hasPermission="bookCart:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
  <a lay-event="pay" shiro:hasPermission="bookCart:pay">
    <i class="layui-icon febs-edit-area febs-gold">&#xe65e;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'formSelects'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-bookCart'),
        formSelects = layui.formSelects,
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'createTime', type: null},
        tableIns;

    form.render();

    initTable();

    laydate.render({
      elem: '#bookCart-createTime',
      range: true,
      trigger: 'click'
    });

    formSelects.render();
    formSelects.config('insure-type', {
      searchUrl: ctx + 'bookMessage/dlist',
      response: {
        statusCode: 200
      },
      beforeSuccess: function (id, url, searchVal, result) {
        var data = result.data;
        var tranData = [];
        for (var i = 0; i < data.length; i++) {
          tranData.push({
            name: data[i].type,
            value: data[i].type
          })
        }
        result.data = tranData;
        return result;
      },
      error: function (id, url, searchVal, err) {
        console.error(err);
        febs.alert.error('获取书籍类型失败');
      }
    });

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('bookCartTable');
        if (name === 'pay') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请先选择要购买的书籍！');
          } else {
            febs.modal.confirm('支付', '确认付款', function () {
              var bookCartIds = [];
              layui.each(checkStatus.data, function (key, item) {
                bookCartIds.push(item.id)
              });
              paybookCarts(bookCartIds.join(','));
            });
          }
        }
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的记录');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条？', function () {
              var bookCartIds = [];
              layui.each(checkStatus.data, function (key, item) {
                bookCartIds.push(item.id)
              });
              deletebookCarts(bookCartIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'pay',
        title: '进行支付',
        perms: 'bookCart:pay'
      }, {
        name: 'delete',
        title: '删除书籍',
        perms: 'bookCart:delete'
      }]
    });
    let flag = 0;
    var dataid = [];
    table.on('tool(bookCartTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('删除书籍信息', '确定删除该书籍信息？', function () {
          deletebookCarts(data.id);
        });
      }
      if (layEvent === 'edit') {
        febs.modal.open('修改购买数量', 'business/bookCart/update/' + data.id, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#bookCart-update').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
      if (layEvent === 'love') {
        febs.post(ctx + 'bookCart/like/' + data.id, null, function () {
          febs.alert.success('收藏成功');
          $query.click();
        });
      }
      if (layEvent === 'pay') {
        febs.modal.confirm('支付', '确认付款', function () {
          febs.post(ctx + 'bookCart/pay/' + data.id, null, function () {
            febs.alert.success('支付成功');
            $query.click();
          });
        });
      }
    });


    table.on('sort(bookCartTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'bookCartTable',
        url: ctx + 'bookCart/list',
        cols: [[
          {type: 'checkbox'},
          {field: 'bookname', title: '图书名称', minWidth: 100},
          {title: '出版社', field: 'pubid'},
          {title: '图书类型', field: 'type'},
          {title: '书籍简介', field: 'message'},
          {field: 'bookmoney', title: '单价'},
          {field: 'number', title: '数量', sort: true},
          {title: '操作', toolbar: '#bookCart-option-one', minWidth: 140}
        ]]
      });
    }

    function getQueryParams() {
      return {
        bookname: $searchForm.find('input[name="bookname"]').val().trim(),
        type: $searchForm.find('input[name="type"]').val().trim(),
      };
    }

    function deletebookCarts(bookCartIds) {
      febs.post(ctx + 'bookCart/delete/' + bookCartIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }

    function paybookCarts(bookCartIds) {
      febs.post(ctx + 'bookCart/pay/' + bookCartIds, null, function () {
        febs.alert.success('支付成功');
        $query.click();
      });
    }
  })
</script>